package fpsf
{	
	public class Sitemap
	{
		private var map:Array;
		
		private var externalImport:Array;
		private var externalImportConversion:Function;
		
		public function Sitemap()
		{
			map = new Array();
		}
		
		public function add(fragment:String,state:String, title:String):void
		{
			map.push(new Array(fragment,state,title));
		}
		
		public function getFragment(i:int):String
		{
			return map[i][0];
		}
		
		public function getState(i:int):String
		{
			return map[i][1];
		}
		
		public function getTitle(i:int):String
		{
			return map[i][2];
		}
	
		public function asArray():Array
		{
			return map;
		}
		
		public function importFromExternalSource(url:String, convertToArray:Function = Sitemap.externalImportXMLConversion):void
		{
			externalImportArray = new Array();
			externalImportConversion = convertToArray;
			new ExternalLoading(url,externalImportArray,importFromExternalSource2);
		}
		
		private function importFromExternalSource2()
		{
			this.importFromArray(externalImportConversion(externalImportArray[0]));
		}
		
		public static function externalImportXMLConversion(s:String):Array
		{
			var xmlresult:XML = new XML(s);
			var temp:Array = new Array();
			for each (var xl:XMLList in xmlresult.children()) {
				temp.push(new array(xl.fragment, xl.state, xl.title))
			}
			return temp;
		}
		
		public function importFromArray(a:Array):void
		{
			for each(var node:Array in a) {
				if (node.length == 3) {
					this.add(node[0],node[1],node[2]);
				}
			}
		}
		
	}
}